$(function () {

    var up = $("#up");
    var down = $("#down");
    var first = $("#first");
    var second = $("#second");
    var third = $("#third");
    var last = $("#last");
    var currentNo = $("#pageNo");
    var pageNo = 1;

    var p1 = first.attr("name");
    var p2 = second.attr("name");
    var p3 = third.attr("name");
    var form = $("#form");

    up.bind("click",function () {
        pageNo = Number(currentNo.val())-1;
       if(pageNo>1){
           currentNo.val(pageNo);
           sub(pageNo)
       }else {
           currentNo.val(1);
           sub(1);
       }
    });

    down.bind("click",function () {
        pageNo = Number(currentNo.val())+1;
       if(pageNo<last.attr("name")){
           currentNo.val(pageNo);
           sub(pageNo);
       } else{
           currentNo.val(last.attr("name"));
           sub(last.attr("name"));
       }
    });

    last.bind("click",function () {
        var vlast = last.attr("name");
        sub(vlast);
    });

    first.bind("click",function () {
       currentNo.val(1);
       sub(p1);
    });

    second.bind("click",function () {
        currentNo.val(p2);
        sub(p2);
    });

    third.bind("click",function () {
       currentNo.val(p3);
       sub(p3);
    });

    function typeTransfer(type){
        if(type== "1"){
            return "系统管理员";
        }else {
            return "普通用户";
        }
    }


    function sexTransfer(sex){
        if(sex== "1"){
            return "男";
        }else {
            return "女";
        }
    }
    //默认获取前五条数据
    $.ajax({
        url : "easybuy/user/userList",
        type : "GET",
        data :  {"pageIndex":pageNo},
        dataType : "json",
        success : function (data) {
            last.attr("name",data.pageCount);
            var th = "<table class='mem_tab' style=\"width:870px; text-align:center; margin-top:20px;\" cellspacing=\"0\" cellpadding=\"0\">";
            th +="<tr style='font-size: 20px;height: 40px'><td>用户名称</td>";
            th +="<td width='145'>真实姓名</td>";
            th +="<td width='145'>性别</td>";
            th +="<td width='145'>类型</td>";
            th +="<td colspan='2' width='145'>操作</td></tr>";
            form.append(th);
            for(var i=0;i<data.list.length;i++){
                form.append("<tr><td>"+data.list[i].loginName+"</td>"+
                                "<td>"+data.list[i].userName+"</td>"+
                                "<td>"+sexTransfer(data.list[i].sex)+"</td>"+
                                "<td>"+typeTransfer(data.list[i].type)+"</td>"+
                                "<td>"+"<a href='javascript:void(0)' id='del' onclick='del("+data.list[i].id+")'>"+view(data.list[i].loginName)+"</a>"+"</td>"+
                                "<td>"+"<a href='Member_Update.html?id="+data.list[i].id+"'>修改</a>"+"</td></tr></table>"
                );
            }
        },
        error : function () {
            alert("出现错误");
        }
    });
    function  view(loginName) {
        if(loginName=="admin"){
            return "";
        }else{
            return "删除";
        }
    }
    function sub(pageNo){
        form.html("");
        //alert($("#pageNo").val());
        $.ajax({
            url : "easybuy/user/userList",
            type : "GET",
            data : {"pageIndex":pageNo},
            dataType: "json",
            success : function(data){
                var th = "<table class='mem_tab' style=\"width:870px; text-align:center; margin-top:20px;\" cellspacing=\"0\" cellpadding=\"0\">";
                    th +="<tr style='font-size: 20px;height: 40px'><td>用户名称</td>";
                    th +="<td width='145'>真实姓名</td>";
                    th +="<td width='145'>性别</td>";
                    th +="<td width='145'>类型</td>";
                    th +="<td colspan='2' width='145'>操作</td></tr>";
                    form.append(th);
                for(var i=0;i<data.list.length;i++) {
                    form.append("<tr><td>" + data.list[i].loginName + "</td>" +
                        "<td>" + data.list[i].userName + "</td>" +
                        "<td>" + sexTransfer(data.list[i].sex) + "</td>" +
                        "<td>" + typeTransfer(data.list[i].type) + "</td>" +
                        "<td>" + "<a href='javascript:void(0)' class='del' onclick='del("+data.list[i].id+")'>删除</a>" + "</td>" +
                        "<td>" + "<a href='Member_Update.html?id=" + data.list[i].id + "'>修改</a>" + "</td></tr></table>"
                    );
                }
            },
            error : function(){
                alert("出现错误");
            }
        });
    }

});
function del(id) {
    $.ajax({
        url : "easybuy/user/delete",
        type : "get",
        data : {id : id},
        dataType : "json",
        success : function(data) {
            if (data > 0) {
                location.reload();
            }
        },
        error : function () {
            alert("出现错误");
        }
    });
}